{% extends base.html %}
<!--
@author xupingmao
@since 2020/01/06
@modified 2020/03/29 21:05:39
-->

{% block body %}

<div class="grid-row note-list">

    <!-- header -->
    <div class="row card-title">
        <a href="{{parent.url}}" class="link2 path-link">{{parent.name}}</a>&nbsp;/&nbsp;<span>管理</span>

        <div class="float-right">
            {% if parent_id != "0" %}
                <button class="reverse-select-btn">反选</button>
                <button class="move-btn" data-url="/note/group/select?callback=batchMoveNoteCallback">移动</button>
            {% end %}
        </div>
    </div>

    {% for item in files %}
        <div class="book-item">
            {% if item.priority>0 %}
                <i class="fa fa-thumb-tack black"></i>
            {% end %}

            <i class="fa {{item.icon}} fa-{{item.icon}} black"></i>                
            <a class="link2" href="{{item.url}}">{{item.name}}</a>
            
            <span class="float-right">
                <a class="item-option" data-id="{{item.id}}" data-name="{{item.name}}" 
                    onclick="renameNoteByAttr(this);" href="javascript:void(0);">重命名</a>
                <input type="checkbox" data-id="{{item.id}}" data-name="{{item.name}}"/>
            </span>
        </div>
    {% end %}
</div>

{% include note/component/batch_move.html %}
{% include note/script/rename_script.html %}

<script type="text/javascript">
$(function () {
    $(".reverse-select-btn").click(function () {
        $("input[type=checkbox]").each(function (index, element) {
            var value = $(element).prop("checked");
            $(element).prop("checked", !value);
        });
    });

    function doMoveTo(idList, selfId, parentId) {
        $.post("/note/move", 
          {id:selfId, parent_id: parentId}, 
          function (resp){
              console.log(resp);
              // 不需要关心哪一个，只是用于计数
              idList.pop();

              if (idList.length == 0) {
                window.location.reload();
              }
        });
    }

    window.batchMoveNoteCallback = function (selectedId) {
        var checked = $(":checked");
        var idList = [];

        checked.each(function (index, element) {
            var selfId = $(element).attr("data-id");
            idList.push(selfId);

            doMoveTo(idList, selfId, selectedId);
        });
    };

    $(".rename-btn").click(function(event) {
        console.log("rename");
        var checked = $(":checked");
        if (checked.length == 0) {
            alert("请选择笔记");
            return;
        }

        if (checked.length != 1) {
            alert("不支持批量重命名");
            return;
        }

        renameNote(checked.attr("data-id"), checked.attr("data-name"));
    });

});
</script>

{% end %}

